package com.ruoyi.web.controller.server;

import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.constant.MessageRedisConstant;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysEnterprise;
import com.ruoyi.system.domain.SysPosition;
import com.ruoyi.system.service.ISysEnterpriseService;
import com.ruoyi.system.service.ISysPositionService;
import com.sun.jna.platform.win32.WinBase;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.NumberUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDialogue;
import com.ruoyi.system.service.ISysDialogueService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 对话Controller
 * 
 * @author ruoyi
 * @date 2025-03-09
 */
@RestController
@RequestMapping("/system/dialogue")
public class SysDialogueController extends BaseController
{
    @Autowired
    private ISysDialogueService sysDialogueService;

    @Autowired
    private RedisCache redisCache ;

    /**
     * 查询对话列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysDialogue sysDialogue)
    {
        startPage();
        List<SysDialogue> list = sysDialogueService.selectSysDialogueList(sysDialogue);

        return getDataTable(list);
    }

    @GetMapping("/getUnReadNumber")
    public AjaxResult getUnReadNumber() {
        Integer cacheObject = redisCache.getCacheObject(MessageRedisConstant.CURRENT_USER_UN_READ_MESSAGE_NUMBER + SecurityUtils.getUserId());
        if (ObjectUtils.isNotEmpty(cacheObject)) {
            return AjaxResult.success(cacheObject) ;
        }else {
            return AjaxResult.success(0) ;
        }
    }

    @PostMapping("/jumpToContent")
    public TableDataInfo jumpToContent(@RequestBody SysDialogue dialogue) {
        List<SysDialogue> list = sysDialogueService.jumpToContent(dialogue);
        return getDataTable(list);
    }

    /**
     * 导出对话列表
     */
    @Log(title = "对话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDialogue sysDialogue)
    {
        List<SysDialogue> list = sysDialogueService.selectSysDialogueList(sysDialogue);
        ExcelUtil<SysDialogue> util = new ExcelUtil<SysDialogue>(SysDialogue.class);
        util.exportExcel(response, list, "对话数据");
    }

    /**
     * 获取对话详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysDialogueService.selectSysDialogueById(id));
    }

    /**
     * 新增对话
     */
    @Log(title = "对话", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDialogue sysDialogue)
    {
        return toAjax(sysDialogueService.insertSysDialogue(sysDialogue));
    }

    /**
     * 修改对话
     */
    @Log(title = "对话", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDialogue sysDialogue)
    {
        return toAjax(sysDialogueService.updateSysDialogue(sysDialogue));
    }

    /**
     * 删除对话
     */
    @Log(title = "对话", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysDialogueService.deleteSysDialogueByIds(ids));
    }
}
